#------------------------------------------------------------------------------
# clean RCMS data
#==============================================================================
rcms = as.data.table(import(file.path(rawdata_path, 'RCMS', "Longitudinal Religious Congregations and Membership File, 1980-2010 (County Level).XLSX")))

# fix county fips coding
rcms[,county_fips := stringr::str_pad(FIPSMERG,width=5,pad='0')]

rcms_reltrad = rcms[YEAR == 2010,.(N_ADHERENT = sum(ADHERENT,na.rm=TRUE)),by=c('county_fips','RELTRAD')]
rcms_reltrad[,RELTRAD := factor(RELTRAD,levels = 1:6, labels=c('GC_ProE','GC_ProM','GC_Cath','GC_Oth','GC_Jew','GC_ProB'))]

rcms_reltrad = dcast(rcms_reltrad,county_fips~RELTRAD, value.var = 'N_ADHERENT')
rel_var = grep('GC_',names(rcms_reltrad),value=TRUE)
for (var in rel_var) {
	rcms_reltrad[is.na(get(var)),(var) := 0]
}

# total population from RCMS file
rcms_pop = unique(rcms[YEAR == 2010,c('county_fips','TOTPOP')])
rcms_reltrad = merge(rcms_reltrad,rcms_pop, by='county_fips',all.x=TRUE, all.y=TRUE)

# Save the dataset
write_fst(rcms_reltrad, file.path(processed_path,"rcms_reltrad.fst"), 100)
    
